home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_7_2.arc / SEMAPHOR.HXX < prev    next >
Text File  |  1988-12-21  |  3KB  |  119 lines

  1. // semaphor.hxx
  2. //
  3. // defines the OS/2 semaphore class for IPC
  4. //
  5. // author: vaughn vernon
  6. // (c) Copyright 1988 Aspen Scientific
  7. // All Rights Reserved.
  8.  
  9. #ifndef __SEMCLASS__
  10. # define __SEMCLASS__
  11. # ifndef APIENTRY
  12. #    include <os2def.h>
  13. # endif
  14. # ifndef INCL_DOS
  15. #    define    INCL_DOS
  16. #    include <bsedos.h>
  17. # endif
  18.  
  19. // the semaphore class definition
  20.  
  21. class Semaphore {
  22.  
  23.     union    {
  24.         ULONG    RAM;        // a RAM Semaphore
  25.         HSYSSEM    SYS;        // a System Semaphore
  26.     } Sem;
  27.  
  28.     HSEM    SemH;            // this actual handle
  29.     USHORT    returnCode;        // a good create/open?
  30.  
  31.     // a private error routine
  32.     VOID    ErrorOp( CHAR *s )    {
  33.         cout << "\nclass: Semaphore: bad operator: " << s << "\n";
  34.     }
  35.  
  36.     // the class methods
  37. public:
  38.  
  39.     // constructor and destructor
  40.  
  41.     // system sem. constructor
  42.     Semaphore( PSZ sysSemName, USHORT owner, BOOL create ) {
  43.  
  44.         if ( create )
  45.             returnCode = DosCreateSem(
  46.                     (owner ? CSEM_PRIVATE:CSEM_PUBLIC),
  47.                     (PHSYSSEM) &Sem.SYS,
  48.                     sysSemName );
  49.         else
  50.             returnCode = DosOpenSem( (PHSYSSEM) &Sem.SYS,
  51.                     sysSemName );
  52.  
  53.         SemH = Sem.SYS;        // use system handle
  54.     }
  55.  
  56.     // RAM sem. constructor
  57.     Semaphore( )        {
  58.  
  59.         Sem.RAM = 0L;        // initialize
  60.         SemH = &Sem.RAM;    // handle is address; use RAM handle
  61.         returnCode = 0;    
  62.     }
  63.  
  64.     // destructor may have to close the system resource
  65.     ~Semaphore( )        {
  66.  
  67.         if ( Sem.SYS != HSYSSEM(0) )
  68.             DosCloseSem( Sem.SYS );
  69.     }
  70.  
  71.     // control methods
  72.  
  73.     // was the open/create valid?
  74.     BOOL    GetStatus()    { return (returnCode ? FALSE:TRUE); }
  75.  
  76.     // SetNow is a forced set, usually ment for thread signaling
  77.     VOID    SetNow()    { DosSemSet( SemH ); }
  78.  
  79.     // SetRequest is used to wait on a possibly owned semaphore
  80.     BOOL    SetRequest( LONG timeOut ) {
  81.         return (DosSemRequest( SemH, timeOut ) ? FALSE:TRUE);
  82.     }
  83.  
  84.     // Wait is used to wait on the clearing of a semaphore.
  85.     // this is typically used to wait on a semaphore signal.
  86.     BOOL    Wait( LONG timeOut )    {
  87.         return DosSemWait( SemH, timeOut );
  88.     }
  89.  
  90.     // Fire is used to send a signal to a waiting thread
  91.     // that self-set this semaphore
  92.     VOID    Fire()        { Clear(); }
  93.  
  94.     // Clear is used to clear ownership of this semaphore
  95.     VOID    Clear()        { DosSemClear( SemH ); }
  96.  
  97.     // C++ operator overloading:  implements a signaling
  98.     // environment. "++" sets, and "--" fires.
  99.     VOID    operator++()    { SetNow(); }
  100.     VOID    operator--()    { Fire(); }
  101.  
  102.     // invalid C++ operators for this class: although this
  103.     // is not really necessary, BS recommends it for
  104.     // users of the class other than the inventor.
  105.     // for instance, I sure don't want someone trying to
  106.     // add semaphores!:  sem3 = sem1 + sem2;
  107.  
  108.     VOID    operator+()    { ErrorOp( "'+'" ); }
  109.     VOID    operator-()    { ErrorOp( "'-'" ); }
  110.     VOID    operator+=()    { ErrorOp( "'+='" ); }
  111.     VOID    operator-=()    { ErrorOp( "'-='" ); }
  112.     VOID    operator()()    { ErrorOp( "'()'" ); }
  113.     VOID    operator[]()    { ErrorOp( "'[]'" ); }
  114.     VOID    operator*()    { ErrorOp( "'*'" ); }
  115.     // etc....
  116. };
  117.  
  118. #endif
  119.